import request from '@/utils/request'

// 获取所有交易
export function getAllTransactions() {
  return request({
    url: '/api/transactions',
    method: 'get'
  })
}

// 分页获取交易列表
export function getTransactionsByPage(pageNum, pageSize, query = {}) {
  return request({
    url: '/api/transactions/page',
    method: 'get',
    params: {
      pageNum,
      pageSize,
      ...query
    }
  })
}

// 根据ID获取交易详情
export function getTransactionById(id) {
  return request({
    url: `/api/transactions/${id}`,
    method: 'get'
  })
}

// 获取用户的交易记录
export function getUserTransactions(userId) {
  return request({
    url: `/api/transactions/user/${userId}`,
    method: 'get'
  })
}

// 获取经销商的交易记录
export function getDealerTransactions(dealerId) {
  return request({
    url: `/api/transactions/dealer/${dealerId}`,
    method: 'get'
  })
}

// 获取车辆的交易记录
export function getCarTransactions(carId) {
  return request({
    url: `/api/transactions/car/${carId}`,
    method: 'get'
  })
}

// 添加交易
export function addTransaction(data) {
  return request({
    url: '/api/transactions',
    method: 'post',
    data
  })
}

// 更新交易
export function updateTransaction(data) {
  return request({
    url: '/api/transactions',
    method: 'put',
    data
  })
}

// 更新交易状态
export function updateTransactionStatus(id, status) {
  return request({
    url: `/api/transactions/${id}/status/${status}`,
    method: 'put'
  })
}

// 更新支付状态
export function updatePaymentStatus(id, paymentStatus) {
  return request({
    url: `/api/transactions/${id}/payment/${paymentStatus}`,
    method: 'put'
  })
}

// 删除交易
export function deleteTransaction(id) {
  return request({
    url: `/api/transactions/${id}`,
    method: 'delete'
  })
}

// 批量删除交易
export function batchDeleteTransactions(ids) {
  return request({
    url: `/api/transactions/batch/${ids}`,
    method: 'delete'
  })
}

// 获取支付方式选项
export function getPaymentMethodOptions() {
  return [
    { label: '信用卡', value: '信用卡' },
    { label: '银行转账', value: '银行转账' },
    { label: '微信支付', value: '微信支付' },
    { label: '支付宝', value: '支付宝' },
    { label: '分期付款', value: '分期付款' },
    { label: '现金', value: '现金' }
  ]
}

// 获取支付状态选项
export function getPaymentStatusOptions() {
  return [
    { label: '未支付', value: 0 },
    { label: '已支付', value: 1 },
    { label: '已退款', value: 2 }
  ]
}

// 获取交易状态选项
export function getTransactionStatusOptions() {
  return [
    { label: '进行中', value: 0 },
    { label: '已完成', value: 1 },
    { label: '已取消', value: 2 }
  ]
}

// 格式化支付状态
export function formatPaymentStatus(status) {
  const statusMap = {
    0: '未支付',
    1: '已支付',
    2: '已退款'
  }
  return statusMap[status] || '未知'
}

// 格式化交易状态
export function formatTransactionStatus(status) {
  const statusMap = {
    0: '进行中',
    1: '已完成',
    2: '已取消'
  }
  return statusMap[status] || '未知'
}

// 查询预约列表
export function listAppointment(query) {
  return request({
    url: '/getAllAppointments',
    method: 'get'
  })
}

// 查询预约详细信息
export function getAppointment(id) {
  return request({
    url: '/getAppointment/' + id,
    method: 'get'
  })
}

// 新增预约
export function addAppointment(data) {
  return request({
    url: '/insertAppointment',
    method: 'post',
    data: data
  })
}

// 修改预约
export function updateAppointment(data) {
  return request({
    url: '/updateAppointment',
    method: 'post',
    data: data
  })
}

// 删除预约
export function deleteAppointment(id) {
  return request({
    url: '/deleteAppointment/' + id,
    method: 'get'
  })
}

// 批量删除预约
export function batchDeleteAppointment(ids) {
  const idArray = ids.split(',');
  const promises = idArray.map(id => deleteAppointment(id));
  
  return Promise.all(promises).then(() => {
    return {
      code: 200,
      message: 'success',
      data: null
    }
  });
}

// 导出预约数据
export function exportAppointment(query) {
  return request({
    url: '/exportAppointments',
    method: 'get',
    params: query,
    responseType: 'blob'
  })
} 